<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var \Magento\Braintree\Block\Form $block */
$code = $block->escapeHtml($block->getMethodCode());
$storedCards = $this->helper('\Magento\Braintree\Helper\Createorder')->getLoggedInCustomerCards();
$useVault = $block->useVault();
$useCvv = $block->useCvv();
$clientToken = $block->escapeHtml($block->getClientToken());
$isFraudDetectionEnabled = $block->isFraudDetectionEnabled();
$braintreeDataJs = $block->getBraintreeDataJs();
$formData = [
    "useVault" => $useVault,
    "useCvv"  => $useCvv,
    "clientToken" => $clientToken,
    "code" => $code,
    "isFraudDetectionEnabled" => $isFraudDetectionEnabled,
    "braintreeDataJs"=> $braintreeDataJs,
];
$serializedFormData = $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode($formData);
$ccType = $block->getInfoData('cc_type');
$ccExpMonth = $block->getInfoData('cc_exp_month');
$ccExpYear = $block->getInfoData('cc_exp_year');
?>
<input id="<?php /* @noEscape */ echo $code; ?>_payment_method" type="hidden" name="payment[method]"
       value="<?php /* @noEscape */ echo $code; ?>" />
<div id="payment_form_<?php /* @noEscape */ echo $code; ?>" class="admin__page-section-item" style="display:none;"
    data-mage-init='{"braintreeCcForm":<?php /* @noEscape */ echo $serializedFormData; ?>}'
    >
    <input type="hidden" name="payment[payment_method_nonce]" id="braintree_nonce" value="" />
    <input type="hidden" name="payment[cc_last4]" id="cc_last4" value="" />
    <?php if ($isFraudDetectionEnabled): ?>
        <input type="hidden" name="payment[device_data]" id="braintree_device_id" value="" />
    <?php endif; ?>
    <?php if ($storedCards): ?>
        <fieldset class="admin__fieldset">
            <div class="admin__field" id="<?php /* @noEscape */ echo $code; ?>_token_selector">
                <label class="admin__field-label" for="<?php /* @noEscape */ echo $code; ?>_cc_token">
                    <?php echo $block->escapeHtml(__('Payment Information')); ?>
                </label>
                <div class="admin__field-control control">
                    <select id="<?php /* @noEscape */ echo $code; ?>_cc_token" name="payment[cc_token]"
                            class="select admin__control-select">
                        <?php foreach ($storedCards as $creditCard): ?>
                            <option value="<?php echo $block->escapeHtml($creditCard->token); ?>"
                                <?php /* @noEscape */ echo $creditCard->default ? ' selected="selected"' : ''; ?>>
                                <?php echo $block->escapeHtml($creditCard->maskedNumber); ?> - <?php echo $block->escapeHtml($creditCard->cardType); ?>
                            </option>
                        <?php endforeach; ?>
                        <option value=''><?php echo $block->escapeHtml(__('Add new card')); ?></option>
                    </select>
                </div>
            </div>
        </fieldset>
    <?php endif; ?>
    <fieldset class="admin__fieldset hide_if_token_selected">
    <div class="admin__field">
        <label class="label admin__field-label" for="<?php /* @noEscape */ echo $code; ?>_cc_type" >
            <?php echo $block->escapeHtml(__('Credit Card Type')); ?><span class="required">*</span>
        </label>
        <div class="admin__field-control control">
            <select id="<?php /* @noEscape */ echo $code; ?>_cc_type" name="payment[cc_type]"
                    class="required-entry _required select admin__control-select validate-cc-type-select">
                <option value="">--<?php echo $block->escapeHtml(__('Please Select')); ?>--</option>
            <?php foreach ($block->getCcAvailableTypes() as $typeCode => $typeName): ?>
                <option value="<?php echo $block->escapeHtml($typeCode); ?>"
                    <?php if($typeCode == $ccType): ?> selected="selected"<?php endif; ?>>
                    <?php echo $block->escapeHtml($typeName); ?>
                </option>
            <?php endforeach; ?>
            </select>
        </div>
    </div>
    </fieldset>
    <fieldset class="admin__fieldset hide_if_token_selected">
    <div class="admin__field">
        <label class="label admin__field-label" for="<?php /* @noEscape */ echo $code; ?>_cc_number">
            <?php echo $block->escapeHtml(__('Credit Card Number')); ?><span class="required">*</span>
        </label>
        <div class="admin__field-control control">
            <input type="text" id="<?php /* @noEscape */ echo $code; ?>_cc_number" data-encrypted-name="payment[cc_number]"
                   title="<?php echo $block->escapeHtml(__('Credit Card Number')); ?>"
                   class="input-text admin__control-text validate-cc-number validate-cc-type" value="" />
        </div>
    </div>
    </fieldset>
    <fieldset class="admin__fieldset hide_if_token_selected">
    <div id="<?php /* @noEscape */ echo $code; ?>_cc_type_exp_div" class="admin__field">
        <label class="label admin__field-label" for="<?php /* @noEscape */ echo $code; ?>_expiration">
            <?php echo $block->escapeHtml(__('Expiration Date')); ?><span class="required">*</span>
        </label>
        <div class="admin__field-control control">
            <select id="<?php /* @noEscape */ echo $code; ?>_expiration" name="payment[cc_exp_month]"
                    class="month validate-cc-exp required-entry _required select admin__control-select">
                <?php foreach ($block->getCcMonths() as $k=>$v): ?>
                    <option value="<?php /* @noEscape */ echo $k ? $block->escapeHtml($k) : ''; ?>"
                        <?php if ($k == $ccExpMonth): ?> selected="selected"<?php endif; ?>>
                        <?php echo $block->escapeHtml($v); ?></option>
                <?php endforeach; ?>
            </select>
            <select id="<?php /* @noEscape */ echo $code; ?>_expiration_yr" name="payment[cc_exp_year]"
                    class="year required-entry _required select admin__control-select">
            <?php foreach ($block->getCcYears() as $k => $v): ?>
                <option value="<?php /* @noEscape */ echo $k ? $block->escapeHtml($k) : ''; ?>"
                    <?php if ($k == $ccExpYear): ?> selected="selected"<?php endif; ?>>
                    <?php echo $block->escapeHtml($v); ?>
                </option>
            <?php endforeach ?>
            </select>
        </div>
    </div>
    </fieldset>
    <?php echo $block->getChildHtml(); ?>
    <?php if ($block->hasVerification()): ?>
        <fieldset class="admin__fieldset hide_if_token_selected">
            <div id="<?php /* @noEscape */ echo $code; ?>_cc_type_cvv_div" class="admin__field">
                <label class="label admin__field-label" for="<?php /* @noEscape */ echo $code; ?>_cc_cid">
                    <?php echo $block->escapeHtml(__('Card Verification Number')); ?><span class="required">*</span>
                </label>

                <div class="admin__field-control control">
                    <div class="v-fix">
                        <input type="text" title="<?php echo $block->escapeHtml(__('Card Verification Number')); ?>"
                               class="input-text admin__control-text cvv required-entry validate-cc-cvn"
                               id="<?php /* @noEscape */ echo $code; ?>_cc_cid" data-encrypted-name="payment[cc_cid]" value=""/>
                    </div>
                </div>
            </div>
        </fieldset>
    <?php endif; ?>
    <?php if($useVault): ?>
        <fieldset class="admin__fieldset hide_if_token_selected">
            <div id="<?php /* @noEscape */ echo $code; ?>_store_in_vault_div" style="text-align:left;" class="">
                <input type="checkbox" title="<?php echo $block->escapeHtml(__('Save this card for future use')); ?>"
                       class="input-checkbox" id="<?php /* @noEscape */ echo $code; ?>_store_in_vault"
                       name="payment[store_in_vault]" value="1"/>
                <label for="<?php /* @noEscape */ echo $code; ?>_store_in_vault" style="float:none;">
                    <?php echo $block->escapeHtml(__('Save this card for future use')); ?>

                </label>
            </div>
        </fieldset>
    <?php endif; ?>
</div>
